package com.uikoo9.job;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.uikoo9.manage.y.model.YIdeaModel;
import com.uikoo9.manage.y.util.RecordUtil;
import com.uikoo9.manage.y.util.SqlKit;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设置idea 的level
 * 如
 *            0
 *          1 1  1
 *        2 2  2  2
 *       3 3
 * Created by user on 2014/9/1.
 */


public class JobA implements Job {
    static int callTime = 0;

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        Map map = new HashMap();
        map.put("a.y_idea_parentid",0);
        List<YIdeaModel>list =  YIdeaModel.dao.findRelaProject(map);

        setLevel( list,callTime);
        List<YIdeaModel>listL =  YIdeaModel.dao.findByLastId();
        for(YIdeaModel m:listL){
            m.set("y_idea_level",-1);
            m.update();
        }
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " JobA works,callTime is: " + callTime);
    }

    public void setLevel(List<YIdeaModel> list,int level) {
        try {
            for(YIdeaModel m:list){
                m.set("y_idea_level",level);
                m.update();
            }
            Map map = new HashMap();
            map.put("a.y_idea_parentid",RecordUtil.JsontoList(list, "id"));
            Map querytype = new HashMap();
            String[] ss = {"a.y_idea_parentid"};
            querytype.put(SqlKit.IN,ss);
            List<YIdeaModel>listM =  YIdeaModel.dao.findRelaProject(querytype,map);
            if(listM.size()>0){
                setLevel(listM,++level);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    void print(String msg) {
        System.out.println(msg);
    }

}
